package com.mingqi.enetconnected.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.util.resource.ResourceUtils;

public class HorizontalTextSwitchLayout extends LinearLayout {

    private TextView leftTextView;

    private MultipleTextSwitch textSwitch;

    private String[] textSwitchValue;

    public HorizontalTextSwitchLayout(Context context) {
        this(context, null, 0);
    }

    public HorizontalTextSwitchLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalTextSwitchLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.view_horizontal_layout_text_switch, this);

        leftTextView = findViewById(R.id.left_text);
        textSwitch = findViewById(R.id.text_switch);

        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.HorizontalLayout, defStyleAttr, 0);
        int indexCount = typedArray.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int index = typedArray.getIndex(i);
            if (index == R.styleable.HorizontalLayout_leftText) {
                setLeftText(typedArray.getString(index));
            } else if (index == R.styleable.HorizontalLayout_leftTextColor) {
                setLeftTextViewColor(typedArray.getColor(index, ResourceUtils.getColor(R.color.color_black_333333)));
            } else if (index == R.styleable.HorizontalLayout_textSwitchTabs) {
                textSwitchValue = getResources().getStringArray(typedArray.getResourceId(index, 0));
                setTextSwitch(textSwitchValue);
            }
        }

        typedArray.recycle();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }


    public void setLeftTextViewColor(int color) {
        leftTextView.setTextColor(color);
    }


    public void setLeftText(int text) {
        leftTextView.setText(text);
    }

    public void setLeftText(String text) {
        leftTextView.setText(text);
    }

    public void setTextSwitch(String... tagTexts) {
        textSwitchValue = tagTexts;
        textSwitch.setText(tagTexts);
    }

    public int getSelectedTab() {
        return textSwitch.getSelectedTab();
    }

    public String getSelectedValue() {
        if (textSwitchValue != null && textSwitch.getSelectedTab() < textSwitchValue.length) {
            return textSwitchValue[textSwitch.getSelectedTab()];
        }
        return null;
    }
}
